package main;

import gameHandling.BasicHandling;
import gameHandling.ExtendHandling;
import gameObject.Fence;
import gameObject.Hero;

import java.util.Vector;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;

import base.SpriteContainer;

public class GameManager {

	private int previousX;
	private int previousY;
	private int viewPortY;
	private int vY;
	private boolean isPressed;
	private boolean isDraged;
	private LayerManager gameLayer;

	public final int START_MONEY = 100;
	public final int MAX_LIFE = 5;
	public final int MAX_Y = 360;
	public final int MIN_Y = 30;

	public Vector listHero;
	public Vector listFence;
	public Vector listItem;
	public Hero forcusHero;
	
	public TiledLayer tiledMap;
	public int money;
	public int numLife;
	public int level;

	public ExtendHandling extendHandling;
	public int[][] objectMap;

	public GameManager() {
		viewPortY = 0;
		isPressed = false;
		gameLayer = new LayerManager();
		extendHandling = new BasicHandling();
		money = START_MONEY;
		numLife = MAX_LIFE;
		level = 0;
		int nC = 8;
		int nR = 15;
		tiledMap = new TiledLayer(nC, nR, Res.IMG_MAP, 30, 30);
		objectMap = new int[nC][nR];
		for (int i = 0; i < nC; i++) {
			for (int j = 0; j < nR; j++) {
				int tiledIndex = 1;
				if (Res.getRandom(3) == 0) {
					tiledIndex = Res.getRandom(4) + 1;
				}
				tiledMap.setCell(i, j, tiledIndex);
				objectMap[i][j] = 0;
			}
		}
		Sprite darkFog = new Sprite(Res.IMG_FOG, 240, 50);
		Sprite whiteFog = new Sprite(Res.IMG_FOG, 240, 50);
		whiteFog.setPosition(0, 30*nR-50);
		whiteFog.setFrame(1);
		gameLayer.append(darkFog);
		gameLayer.append(whiteFog);
		gameLayer.append(tiledMap);

		listHero = new Vector();
		listFence = new Vector();
		listItem = new Vector();

	}

	public int getHeroCost(){
		return (listHero.size()*listHero.size())*45+45;
	}
	public void addDrawableObject(SpriteContainer obj) {
		obj.addTo(gameLayer);
	}

	public void removeDrawableObject(SpriteContainer obj) {
		obj.removeFrom(gameLayer);
	}

	public void RemoveHero(Hero hero) {
		objectMap[hero.getColIndex()][hero.getRowIndex()] = 0;
		hero.removeFrom(gameLayer);
		listHero.removeElement(hero);
	}

	public void AddHero(Hero hero, int i, int j) {
		objectMap[i][j] = 1;
		hero.setMapIndex(i, j);
		listHero.addElement(hero);
		hero.addTo(gameLayer);
	}

	public void AddFence(Fence fence, int i, int j) {
		if(i<0 || j<0){
			return;
		}
		listFence.addElement(fence);
		fence.setMapIndex(i, j);
		fence.addTo(gameLayer);
		objectMap[i][j] = 2;

	}

	public void RemoveFence(Fence fence) {
		objectMap[fence.getColIndex()][fence.getRowIndex()] = 0;
		fence.removeFrom(gameLayer);
	}

	public boolean pointerPressed(int x, int y) {
		isDraged = false;
		if (y < MIN_Y || y > MAX_Y) {
			return false;
		}
		previousX = x;
		previousY = y;
		isPressed = true;
		return true;
	}

	public boolean pointerReleased(int x, int y) {
		isPressed = false;
		if (y < MIN_Y || y > MAX_Y) {
			return false;
		}
		if (!isDraged && extendHandling != null) {
			extendHandling.pointerTaped(x, y - getY());
		}else{
		vY = 5 * (y - previousY);
		if (vY > 10)
			vY = 10;
		}
		return true;
	}

	public boolean pointerDragged(int x, int y) {
		if (y < MIN_Y || y > MAX_Y) {
			isPressed = false;
			return false;
		}
		if (!isDraged && Math.abs(x - previousX) + Math.abs(y - previousY) < 20)
			return false;
		setViewPort(viewPortY - y + previousY);
		previousX = x;
		previousY = y;
		isDraged = true;
		return true;
	}

	public void setViewPort(int y) {
		int maxY = tiledMap.getHeight() - MAX_Y + MIN_Y;
		if (y < 0) {
			viewPortY = 0;
		} else if (y > maxY) {
			viewPortY = maxY;

		} else {
			viewPortY = y;
		}
	}

	public int getY() {
		return MIN_Y - viewPortY;
	}

	public void paint(Graphics g) {
		g.fillRect(0, 0, 240, 400);
		gameLayer.paint(g, 0, getY());
		if (extendHandling != null) {
			extendHandling.paint(g);
		}
		if (isPressed || vY != 0) {
			int sH = MAX_Y - MIN_Y;
			int mH = tiledMap.getHeight();
			int h = sH * sH / mH;
			int y = MIN_Y + viewPortY * sH / mH;
			g.setColor(0x114400);
			g.fillRoundRect(230, y, 8, h - 1, 8, 8);
			g.setColor(0x99cc88);
			g.drawRoundRect(230, y, 8, h - 1, 8, 8);
		}
	}

	public void update() {
		int f = 2;
		if (vY > f) {
			vY -= f;
		} else if (vY < -f) {
			vY += f;
		} else {
			vY = 0;
		}
		setViewPort(viewPortY - vY);

		if (extendHandling != null) {
			extendHandling.update();
		}
	}

}
